package cn.xinfei.xdecision.common.service.datax.datainterface.v2.impl;

import cn.xinfei.xdecision.common.dao.datax.datainterface.v2.VariableInterfaceMapper;
import cn.xinfei.xdecision.common.model.datax.datainterface.v2.VariableInterface;
import cn.xinfei.xdecision.common.model.datax.datamanage.v2.Variable;
import cn.xinfei.xdecision.common.model.datax.datamanage.v2.vo.VariableInterfaceVO;
import cn.xinfei.xdecision.common.service.datax.datainterface.v2.VariableInterfaceService;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class VariableInterfaceServiceImpl extends ServiceImpl<VariableInterfaceMapper, VariableInterface> implements VariableInterfaceService {

    @Resource
    VariableInterfaceMapper variableInterfaceMapper;

    @Override
    public List<Long> selectDatasourceIdsByVarcodes(Collection<String> varCodes) {
        if (CollectionUtils.isEmpty(varCodes)) {
            return Collections.emptyList();
        }
        return variableInterfaceMapper.selectDatasourceIdsByVarCodes(varCodes);
    }

    @Override
    public List<String> selectVarCodesByProviderAndOrganId(String provider, Long organId) {
        return variableInterfaceMapper.selectVarCodesByProviderAndOrganId(provider, organId);
    }

    @Override
    public List<String> selectApiByDatasourceId(Integer datasourceId) {
        return variableInterfaceMapper.selectApiByDatasourceId(datasourceId);
    }

    @Override
    public Map<String, List<String>> selectAllApiAndVarCodes() {
        List<VariableInterface> variableInterfaceList = new LambdaQueryChainWrapper<>(variableInterfaceMapper)
                .select()
                .eq(VariableInterface::getDatasourceType, 0)
                .list();
        return Optional.ofNullable(variableInterfaceList)
                .orElseGet(Collections::emptyList)
                .stream()
                .collect(
                        Collectors.groupingBy(VariableInterface::getApi,
                                Collectors.mapping(VariableInterface::getVariableCode,
                                        Collectors.toList())));
    }

    @Override
    public List<VariableInterface> selectAllVarInterfaceRel() {
        List<VariableInterface> variableInterfaceList = new LambdaQueryChainWrapper<>(variableInterfaceMapper)
                .select()
                .eq(VariableInterface::getDatasourceType, 0)
                .list();
        return Optional.ofNullable(variableInterfaceList)
                .orElseGet(Collections::emptyList);
    }

    @Override
    public void batchInsertOrUpdateVariables(List<VariableInterface> variableInterfaces) {
        variableInterfaceMapper.batchInsertOrUpdateVariables(variableInterfaces);
    }

    @Override
    public void save(VariableInterfaceVO variableInterface) {
        variableInterfaceMapper.save(variableInterface);
    }

    @Override
    public void update(VariableInterfaceVO variableInterface) {
        variableInterfaceMapper.update(variableInterface);
    }
}
